// var i;
// var $uI_Setting_UIBindings = require("./UI_Setting_UIBindings");
// var $uIView = require("./UIView");
// var $audioManager = require("./AudioManager");
// var $logManager = require("./LogManager");
// var d = cc._decorator;
// var p = d.ccclass;
// var h = d.property;
// var f = d.inspector;
// var g = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.auto_ui = null;
//         t.startX = 0;
//         t.maxx = 395;
//         t.minx = 0;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype._show = function (e) {
//         var t = [];
//         for (var o = 1; o < arguments.length; o++) {
//             t[o - 1] = arguments[o];
//         }
//         this.initState();
//     };
//     t.prototype._clickListener = function (e) {
//         var t = this.auto_ui;
//         switch (e) {
//             case t.btn_btclose:
//             case t.btn_close:
//                 this._close();
//         }
//     };
//     t.prototype.onLoad = function () {
//         this.auto_ui.txt_idLabel.string = "";
//         if (YPSDK.Platform.loginData && YPSDK.Platform.loginData.userUid) {
//             this.auto_ui.txt_idLabel.string = "ID:" + YPSDK.Platform.loginData.userUid;
//         }
//     };
//     t.prototype.onEnable = function () {
//         this.auto_ui.node_yinxiaoHk.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
//         this.auto_ui.node_yinxiaoHk.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
//         this.auto_ui.node_yinyueHk.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
//         this.auto_ui.node_yinyueHk.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
//     };
//     t.prototype.onTouchStart = function (e) {
//         this.startX = e.getLocationX();
//         $logManager.LogMgr.debug("开始触摸");
//     };
//     t.prototype.onTouchMove = function (e) {
//         $logManager.LogMgr.debug("触摸中");
//         var t = e.getLocationX() - this.startX;
//         var o = e.getCurrentTarget();
//         if ((t > 0 && o.x < this.maxx) || (t < 0 && o.x > this.minx)) {
//             o.x += t;
//         }
//         if (o.x > this.maxx) {
//             o.x = this.maxx;
//         }
//         if (o.x < this.minx) {
//             o.x = this.minx;
//         }
//         var n = o.name;
//         var i = o.x / this.maxx;
//         if ("$node_yinxiaoHk" == n) {
//             this.auto_ui.node_yinxiaopro.getComponent(cc.ProgressBar).progress = i;
//             $audioManager.AudioMgr.setEffectVolume(i);
//         }
//         if ("$node_yinyueHk" == n) {
//             this.auto_ui.node_yinyuepro.getComponent(cc.ProgressBar).progress = i;
//             $audioManager.AudioMgr.setMusicVolume(i);
//         }
//         this.startX = e.getLocationX();
//     };
//     t.prototype.initState = function () {
//         var e = $audioManager.AudioMgr.getSettingInfo().effectVolume;
//         var t = $audioManager.AudioMgr.getSettingInfo().musicVolume;
//         this.auto_ui.node_yinyueHk.x = this.maxx * t;
//         this.auto_ui.node_yinxiaoHk.x = this.maxx * e;
//         this.auto_ui.node_yinyuepro.getComponent(cc.ProgressBar).progress = t;
//         this.auto_ui.node_yinxiaopro.getComponent(cc.ProgressBar).progress = e;
//     };
//     t.prototype.onDisable = function () {};
//     __decorate([h($uI_Setting_UIBindings.default)], t.prototype, "auto_ui", void 0);
//     return __decorate([p], t);
// })($uIView.default);
// exports.default = g;


// import UI_Setting_UIBindings from "./UI_Setting_UIBindings";
// import UIView from "./UIView";
// import {AudioMgr} from "./AudioManager";
// import {LogMgr} from "./LogManager";
// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * The class `SettingsUI` is responsible for handling the game settings UI, dealing with user interactions, 
//  * updating volume changes, and displaying user information.
//  * 建议类名: SettingsUI
//  */
// @ccclass
// export default class UI_Setting extends UIView {

//     @property(UI_Setting_UIBindings)
//     auto_ui: UI_Setting_UIBindings = null;

//     private startX: number = 0;
//     private readonly maxx: number = 395;
//     private readonly minx: number = 0;

//     /**
//      * Displays the settings UI with the initial setup.
//      * @param e - The event that triggers this function.
//      * @param args - Additional arguments.
//      * 建议方法名: show
//      */
//     protected _show(e: any, ...args: any[]): void {
//         this.initState();
//     }

//     /**
//      * Handles button click events within the settings UI.
//      * @param e - The event that triggers this function.
//      * 建议方法名: clickListener
//      */
//     protected _clickListener(e: cc.Button): void {
//         const ui = this.auto_ui;
//         switch (e) {
//             case ui.btn_btclose:
//             case ui.btn_close:
//                 this._close();
//                 break;
//         }
//     }

//     /**
//      * Called when the component is loaded. Initializes the user ID label if available.
//      * 建议方法名: onComponentLoad
//      */
//     onLoad(): void {
//         if (this.auto_ui && this.auto_ui.txt_idLabel) {
//             this.auto_ui.txt_idLabel.string = "";
//             // if (YPSDK.Platform.loginData && YPSDK.Platform.loginData.userUid) {
//             //     this.auto_ui.txt_idLabel.string = `ID: ${YPSDK.Platform.loginData.userUid}`;
//             // }
//         }
//     }

//     /**
//      * Called when the component becomes enabled. Registers touch event listeners.
//      * 建议方法名: onComponentEnable
//      */
//     onEnable(): void {
//         this.auto_ui.node_yinxiaoHk.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
//         this.auto_ui.node_yinxiaoHk.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
//         this.auto_ui.node_yinyueHk.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
//         this.auto_ui.node_yinyueHk.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
//     }

//     /**
//      * Handles touch start events to initialize touch position.
//      * @param event - The touch event.
//      * 建议方法名: handleTouchStart
//      */
//     onTouchStart(event: cc.Event.EventTouch): void {
//         this.startX = event.getLocationX();
//         LogMgr.debug("开始触摸");
//     }

//     /**
//      * Handles touch move events to update the volume sliders.
//      * @param event - The touch event.
//      * 建议方法名: handleTouchMove
//      */
//     onTouchMove(event: cc.Event.EventTouch): void {
//         LogMgr.debug("触摸中");
//         const deltaX = event.getLocationX() - this.startX;
//         const currentNode = event.getCurrentTarget() as cc.Node;

//         if ((deltaX > 0 && currentNode.x < this.maxx) || (deltaX < 0 && currentNode.x > this.minx)) {
//             currentNode.x += deltaX;
//         }
//         if (currentNode.x > this.maxx) {
//             currentNode.x = this.maxx;
//         }
//         if (currentNode.x < this.minx) {
//             currentNode.x = this.minx;
//         }

//         const nodeName = currentNode.name;
//         const progress = currentNode.x / this.maxx;
//         if (nodeName === "$node_yinxiaoHk") {
//             this.auto_ui.node_yinxiaopro.getComponent(cc.ProgressBar).progress = progress;
//             AudioMgr.setEffectVolume(progress);
//         } else if (nodeName === "$node_yinyueHk") {
//             this.auto_ui.node_yinyuepro.getComponent(cc.ProgressBar).progress = progress;
//             AudioMgr.setMusicVolume(progress);
//         }
//         this.startX = event.getLocationX();
//     }

//     /**
//      * Initializes the volume sliders state based on current settings.
//      * 建议方法名: initializeState
//      */
//     initState(): void {
//         const effectVolume = AudioMgr.getSettingInfo().effectVolume;
//         const musicVolume = AudioMgr.getSettingInfo().musicVolume;

//         this.auto_ui.node_yinyueHk.x = this.maxx * musicVolume;
//         this.auto_ui.node_yinxiaoHk.x = this.maxx * effectVolume;

//         this.auto_ui.node_yinyuepro.getComponent(cc.ProgressBar).progress = musicVolume;
//         this.auto_ui.node_yinxiaopro.getComponent(cc.ProgressBar).progress = effectVolume;
//     }

//     /**
//      * Called when the component becomes disabled. Clean up any event listeners here.
//      * 建议方法名: onComponentDisable
//      */
//     onDisable(): void {
//         // No implementation needed for now
//     }
// }

import UISettingUIBindings from "./UI_Setting_UIBindings";
import UIViewBase from "./UIView";
import { AudioMgr } from "./AudioManager";
import { LogMgr } from "./LogManager";
const { ccclass, property, inspector } = cc._decorator;

/**
 * The class `SettingsUI` is responsible for handling the game settings UI, dealing with user interactions, 
 * updating volume changes, and displaying user information.
 * 建议类名: SettingsUI
 */
@ccclass
export default class ViewSetting extends UIViewBase {

    @property(UISettingUIBindings)
    auto_ui: UISettingUIBindings = null;

    private startX: number = 0;
    private readonly maxx: number = 395;
    private readonly minx: number = 0;

    /**
     * Displays the settings UI with the initial setup.
     * @param e - The event that triggers this function.
     * @param args - Additional arguments.
     * 建议方法名: show
     */
    protected _show(e: any, ...args: any[]): void {
        this.initializeState();
    }

    /**
     * Handles button click events within the settings UI.
     * @param e - The event that triggers this function.
     * 建议方法名: clickListener
     */
    protected _clickListener(e: cc.Button): void {
        const uiBindings = this.auto_ui;
        switch (e) {
            case uiBindings.btn_btclose:
            case uiBindings.btn_close:
                this._close();
                break;
        }
    }

    /**
     * Called when the component is loaded. Initializes the user ID label if available.
     * 建议方法名: onComponentLoad
     */
    onLoad(): void {
        const userIDLabel = this.auto_ui?.txt_idLabel;
        // 初始化用户ID标签
        if (userIDLabel) {
            userIDLabel.string = "";
            // if (YPSDK.Platform.loginData && YPSDK.Platform.loginData.userUid) {
            //     userIDLabel.string = `ID: ${YPSDK.Platform.loginData.userUid}`;
            // }
        }
    }

    /**
     * Called when the component becomes enabled. Registers touch event listeners.
     * 建议方法名: onComponentEnable
     */
    onEnable(): void {
        const { node_yinxiaoHk, node_yinyueHk } = this.auto_ui;
        node_yinxiaoHk.on(cc.Node.EventType.TOUCH_START, this.handleTouchStart, this);
        node_yinxiaoHk.on(cc.Node.EventType.TOUCH_MOVE, this.handleTouchMove, this);
        node_yinyueHk.on(cc.Node.EventType.TOUCH_START, this.handleTouchStart, this);
        node_yinyueHk.on(cc.Node.EventType.TOUCH_MOVE, this.handleTouchMove, this);
    }

    /**
     * Handles touch start events to initialize touch position.
     * @param event - The touch event.
     * 建议方法名: handleTouchStart
     */
    handleTouchStart(event: cc.Event.EventTouch): void {
        this.startX = event.getLocationX();
        LogMgr.debug("开始触摸");
    }

    /**
     * Handles touch move events to update the volume sliders.
     * @param event - The touch event.
     * 建议方法名: handleTouchMove
     */
    handleTouchMove(event: cc.Event.EventTouch): void {
        LogMgr.debug("触摸中");
        const deltaX = event.getLocationX() - this.startX;
        const currentNode = event.getCurrentTarget() as cc.Node;

        // 更新节点位置
        if ((deltaX > 0 && currentNode.x < this.maxx) || (deltaX < 0 && currentNode.x > this.minx)) {
            currentNode.x += deltaX;
        }
        currentNode.x = Math.max(this.minx, Math.min(this.maxx, currentNode.x));

        const nodeName = currentNode.name;
        const progress = currentNode.x / this.maxx;
        this.updateVolume(nodeName, progress);

        this.startX = event.getLocationX();
    }

    /**
     * Initializes the volume sliders state based on current settings.
     * 建议方法名: initializeState
     */
    initializeState(): void {
        const { effectVolume, musicVolume } = AudioMgr.getAudioSettings();
        const { node_yinyueHk, node_yinxiaoHk, node_yinyuepro, node_yinxiaopro } = this.auto_ui;

        node_yinyueHk.x = this.maxx * musicVolume;
        node_yinxiaoHk.x = this.maxx * effectVolume;
        node_yinyuepro.getComponent(cc.ProgressBar).progress = musicVolume;
        node_yinxiaopro.getComponent(cc.ProgressBar).progress = effectVolume;
    }

    /**
     * Called when the component becomes disabled. Clean up any event listeners here.
     * 建议方法名: onComponentDisable
     */
    onDisable(): void {
        // No implementation needed for now
    }

	/**
     * Updates the volume based on the node name and progress value.
     * @param nodeName - The name of the volume control node.
     * @param progress - The progress value (0 to 1) indicating the volume level.
     * 建议方法名: updateVolume
     */
    private updateVolume(nodeName: string, progress: number): void {
        if (nodeName === "$node_yinxiaoHk") {
            this.auto_ui.node_yinxiaopro.getComponent(cc.ProgressBar).progress = progress;
            AudioMgr.setSoundEffectVolume(progress);
        } else if (nodeName === "$node_yinyueHk") {
            this.auto_ui.node_yinyuepro.getComponent(cc.ProgressBar).progress = progress;
            AudioMgr.setBackgroundMusicVolume(progress);
        }
    }
}